package com.wanpinghui.supplier.user.company;

import android.app.Activity;
import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.wanpinghui.supplier.R;
import com.wanpinghui.supplier.common.BaseActivity;
import com.wanpinghui.supplier.user.company.adapter.DevelopmentPathAdapter;
import com.wanpinghui.wphlibrary.WPHSdk;
import com.wanpinghui.wphlibrary.common.dialog.LoadingDialog;
import com.wanpinghui.wphlibrary.model.business.BusinessService;
import com.wanpinghui.wphlibrary.model.user.UserInfoService;
import com.wanpinghui.wphlibrary.model.user.bean.CaseBean;
import com.wanpinghui.wphlibrary.model.user.bean.CompanyBean;
import com.wanpinghui.wphlibrary.model.user.bean.CourseBean;
import com.wanpinghui.wphlibrary.photovideo.bean.BaseUrlBean;
import com.wanpinghui.wphlibrary.photovideo.bean.UploadFileBean;
import com.wanpinghui.wphlibrary.photovideo.interfaces.OnActionListener;
import com.wanpinghui.wphlibrary.photovideo.view.ClipActivity;
import com.wanpinghui.wphlibrary.photovideo.view.ProductImageUploadView;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;

import java.util.ArrayList;
import java.util.List;

import me.nereo.multi_image_selector.PhotoPickManager;
import retrofit.callback.YRequestCallback;

/**
 * Created by tg596 on 2018/1/25.
 */
@ContentView(R.layout.layout_activity_company_case)
public class CompanyCaseCourseActivity extends BaseActivity {

    private static final int REQUEST_ADD_CASE = 0x14;

    private static final int REQUEST_DEVELOPMENT_PATH = 0x12;

    @ViewInject(R.id.mViewBacktrack)
    private ImageView mViewBacktrack;

    @ViewInject(R.id.piuv_remark_image)
    private ProductImageUploadView piuv_remark_image;

    @ViewInject(R.id.tv_add_developmentpath)
    private TextView tv_add_developmentpath;

    @ViewInject(R.id.btn_next)
    private TextView btn_next;

    @ViewInject(R.id.recyclerview)
    private RecyclerView recv_developmentpath;

    @ViewInject(R.id.tv_warning_text)
    private TextView tv_warning_text;

    private DevelopmentPathAdapter developmentPathAdapter;

    private static final int REQUEST_SELECT_PHOTO = 0x11;
    private static final int CLIP_PHOTO = 0x13;
    private ArrayList<UploadFileBean> mImageList;//图片

    private List<CaseBean> caseBeanList;

    private List<CourseBean> courseBeanList;

    private CompanyBean bean;
    @Override
    public String getName() {
        return null;
    }

    @Override
    public boolean isBar() {
        return false;
    }

    @Override
    public void initViews() {

        piuv_remark_image.setAddContentString("+添加案例");
    }

    @Override
    public void bindEvents() {
        piuv_remark_image.setOnActionListener(new OnActionListener() {
            @Override
            public void onItemDelete(int position) {

                mImageList.remove(position);
                piuv_remark_image.setData(mImageList);
                //  notifyImageDataChange();
            }

            @Override
            public void onItemClick(int position) {
                Intent intent = new Intent(CompanyCaseCourseActivity.this,AddCaseActivity.class);

                intent.putExtra("bean",caseBeanList.get(position));
                startActivityForResult(intent,REQUEST_ADD_CASE);
            }

            @Override
            public void onAddMoreClick() {
                startActivityForResult(new Intent(CompanyCaseCourseActivity.this,AddCaseActivity.class),REQUEST_ADD_CASE);
            //    PhotoPickManager.selectPicNoCamera(CompanyCaseCourseActivity.this, REQUEST_SELECT_PHOTO, 9, false, mImageList == null ? 0 : mImageList.size());
            }

            @Override
            public void onItemPositionChange() {

            }
        });
        tv_add_developmentpath.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivityForResult(new Intent(CompanyCaseCourseActivity.this,DevelopmentPathActivity.class),REQUEST_DEVELOPMENT_PATH);
            }
        });
        btn_next.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(final View v) {
                final CompanyBean companyBean = new CompanyBean();
                if (bean != null)
                    companyBean.setId(bean.getId());
                companyBean.setCourseList(courseBeanList);
                if (mImageList != null && mImageList.size()>0){
                    LoadingDialog.Build(CompanyCaseCourseActivity.this).setContent("正在上传").show();
                    WPHSdk.getService(BusinessService.class).uploadPictures(mImageList, new YRequestCallback<List<BaseUrlBean>>() {
                        @Override
                        public void onSuccess(List<BaseUrlBean> var1) {
                            if (var1 != null && var1.size()>0){
                                for (int i = 0 ; i< var1.size(); i++){
                                    caseBeanList.get(i).setPictures(var1.get(i).getUrl());
                                }
                                companyBean.setCaseList(caseBeanList);
                                saveCompanyCaseAndCourse(companyBean);
                            }
                        }

                        @Override
                        public void onFailed(int var1, String message) {
                            showWarningText(message);
                            LoadingDialog.dismissDialog();
                        }

                        @Override
                        public void onException(Throwable var1) {
                            LoadingDialog.dismissDialog();
                        }
                    });
                }else {

                    saveCompanyCaseAndCourse(companyBean);
                }



            }
        });
        mViewBacktrack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void saveCompanyCaseAndCourse(CompanyBean companyBean) {
        WPHSdk.getService(UserInfoService.class)
                .saveCompanyCaseAndCourse(companyBean, new YRequestCallback<CompanyBean>() {
                    @Override
                    public void onSuccess(CompanyBean var1) {
                        Intent intent = new Intent(CompanyCaseCourseActivity.this, ServiceSpecialtyActivity.class);
                        if (bean != null){
                            intent.putExtra("bean",bean);
                        }
                        startActivity(intent);
                        LoadingDialog.dismissDialog();
                    }

                    @Override
                    public void onFailed(int var1, String message) {
                        showWarningText(message);
                        LoadingDialog.dismissDialog();
                    }

                    @Override
                    public void onException(Throwable var1) {
                        LoadingDialog.dismissDialog();
                    }
                });
    }

    @Override
    public void initData() {
        developmentPathAdapter = new DevelopmentPathAdapter(this);
        developmentPathAdapter.setEdit(true);
        recv_developmentpath.setLayoutManager(new LinearLayoutManager(this));
        recv_developmentpath.setNestedScrollingEnabled(false);
        recv_developmentpath.setAdapter(developmentPathAdapter);

        bean = getIntent().getParcelableExtra("bean");


        if (bean != null){
            if (bean.getAudit_status() == 21 ) {
                tv_warning_text.setVisibility(View.VISIBLE);
                tv_warning_text.setText(bean.getRemark());
            }


            courseBeanList = bean.getCourseList();
            if (courseBeanList != null && courseBeanList.size() > 0 ){
                developmentPathAdapter.setDatas(courseBeanList);
            }
            caseBeanList = bean.getCaseList();
            if (caseBeanList!= null && caseBeanList.size()>0){
                for (CaseBean bean : caseBeanList){
                    if (mImageList == null) {
                        mImageList = new ArrayList<>();
                    }
                    List<String> list = new ArrayList<>();
                    list.add(bean.getPictures());
                    mImageList.addAll(UploadFileBean.create(list));
                    piuv_remark_image.setData(mImageList);

                }
            }

        }

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, final Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case REQUEST_SELECT_PHOTO:
                if (data != null && resultCode == Activity.RESULT_OK) {
                    Intent intent = new Intent(CompanyCaseCourseActivity.this, ClipActivity.class);
                    intent.putExtra("data", PhotoPickManager.getPhoto(data));
                    startActivityForResult(intent, CLIP_PHOTO);
                }
                break;
            case CLIP_PHOTO:
                if (resultCode == Activity.RESULT_OK && data != null) {
                    if (data.getParcelableArrayListExtra("result") != null && data.getParcelableArrayListExtra("result").size() > 0) {
                        if (mImageList == null) {
                            mImageList = new ArrayList<>();
                        }
                        ArrayList list = data.getParcelableArrayListExtra("result");
                        mImageList.addAll(list);
                        piuv_remark_image.setData(mImageList);
                    }
                }
                break;
            case REQUEST_ADD_CASE:
                if (data != null && resultCode == Activity.RESULT_OK) {
                    CaseBean  caseBean = data.getParcelableExtra("bean");

                    if (caseBean != null){
                        if (caseBeanList == null){
                            caseBeanList = new ArrayList<>();
                        }
                        if (bean != null){
                            caseBean.setCompany_id(bean.getId());
                        }
                        caseBeanList.add(caseBean);
                        if (mImageList == null) {
                            mImageList = new ArrayList<>();
                        }
                        List<String> list = new ArrayList<>();
                        list.add(caseBean.getPictures());
                        mImageList.addAll(UploadFileBean.create(list));
                        piuv_remark_image.setData(mImageList);
                    }
                }

                break;
            case REQUEST_DEVELOPMENT_PATH :
                if (data != null && resultCode == Activity.RESULT_OK) {
                    CourseBean courseBean = data.getParcelableExtra("bean");

                    if (courseBean != null){
                        if (bean != null){
                            courseBean.setCompany_id(bean.getId());
                        }
                        if (courseBeanList == null){
                            courseBeanList = new ArrayList<>();
                        }
                        courseBeanList.add(courseBean);
                        developmentPathAdapter.setDatas(courseBeanList);
                    }
                }
                break;
        }
    }

}
